package com.shsh.safemanage.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.shsh.safemanage.util.Constants;

public class WeakReport implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2536143168869855209L;

	private Long id;
	private int year;
	private int month;
	private Long depart1ID;
	private Long equipID;
	private Long userID;
	private Long problemID;

	private int reportType;

	private List<WeakUserReport> yixianReportList;

	private List<WeakUserReport> guanliReportList;

	private List<WeakUserReport> notWrittenGuanliReportList;

	private List<WeakProblem> needDeleteList;
	private Map<Long, User> userMap;

	private int pointSum;
	private int userCount;
	private int pointAverage;

	public static void setPointTable(WeakReport report) {
		int pointSum = 0;
		int userCount = 0;
		int pointAverage = 0;
		for (WeakUserReport userReport : report.getGuanliReportList()) {
			if (userReport.getProblemList().size() > 0) {
				userCount++;
				pointSum += userReport.getPointSum();
			}
		}
		for (WeakUserReport userReport : report.getYixianReportList()) {
			if (userReport.getProblemList().size() > 0) {
				userCount++;
				pointSum += userReport.getPointSum();
			}
		}
		for (WeakUserReport userReport : report.getNotWrittenGuanliReportList()) {
			if (userReport.getProblemList().size() > 0) {
				userCount++;
				pointSum += userReport.getPointSum();
			}
		}

		if (userCount != 0) {
			pointAverage = pointSum / userCount;
		}

		report.setPointAverage(pointAverage);
		report.setPointSum(pointSum);
		report.setUserCount(userCount);
	}

	public int getPointSum() {
		return pointSum;
	}

	public void setPointSum(int pointSum) {
		this.pointSum = pointSum;
	}

	public int getUserCount() {
		return userCount;
	}

	public void setUserCount(int userCount) {
		this.userCount = userCount;
	}

	public int getPointAverage() {
		return pointAverage;
	}

	public void setPointAverage(int pointAverage) {
		this.pointAverage = pointAverage;
	}

	public int getReportType() {
		return reportType;
	}

	public void setReportType(int reportType) {
		this.reportType = reportType;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public Long getDepart1ID() {
		return depart1ID;
	}

	public void setDepart1ID(Long depart1ID) {
		this.depart1ID = depart1ID;
	}

	public Long getEquipID() {
		return equipID;
	}

	public void setEquipID(Long equipID) {
		this.equipID = equipID;
	}

	public Long getUserID() {
		return userID;
	}

	public void setUserID(Long userID) {
		this.userID = userID;
	}

	public Long getProblemID() {
		return problemID;
	}

	public void setProblemID(Long problemID) {
		this.problemID = problemID;

		if (getReportType() == Constants.WEAK_REPORT_TYPE_YIXIANYUANGONG) {
			for (WeakUserReport userReport : getYixianReportList()) {
				userReport.setProblemID(problemID);
			}
		} else if (getReportType() == Constants.WEAK_REPORT_TYPE_GUANLIRENYUAN) {
			for (WeakUserReport userReport : getGuanliReportList()) {
				userReport.setProblemID(problemID);
			}
		}

	}

	public List<WeakUserReport> getYixianReportList() {
		if (yixianReportList == null) {
			yixianReportList = new ArrayList<WeakUserReport>();
		}

		Collections.sort(yixianReportList, WeakUserReportComparatorByPointSum.getInstance());
		return yixianReportList;
	}

	public void setYixianReportList(List<WeakUserReport> yixianReportList) {
		this.yixianReportList = yixianReportList;
	}

	public List<WeakUserReport> getNotWrittenGuanliReportList() {
		if (notWrittenGuanliReportList == null) {
			notWrittenGuanliReportList = new ArrayList<WeakUserReport>();
		}
		Collections.sort(notWrittenGuanliReportList, UserReportComparatorByRoleLevel.getInstance());
		return notWrittenGuanliReportList;
	}

	public void setNotWrittenGuanliReportList(List<WeakUserReport> notWrittenGuanliReportList) {
		this.notWrittenGuanliReportList = notWrittenGuanliReportList;
	}

	public List<WeakUserReport> getGuanliReportList() {
		if (guanliReportList == null) {
			guanliReportList = new ArrayList<WeakUserReport>();
		}

		Collections.sort(guanliReportList, UserReportComparatorByRoleLevel.getInstance());
		return guanliReportList;
	}

	public void setGuanliReportList(List<WeakUserReport> guanliReportList) {
		this.guanliReportList = guanliReportList;
	}

	public List<WeakProblem> getNeedDeleteList() {
		if (needDeleteList == null) {
			needDeleteList = new ArrayList<WeakProblem>();
		}
		return needDeleteList;
	}

	public void setNeedDeleteList(List<WeakProblem> needDeleteList) {
		this.needDeleteList = needDeleteList;
	}

	public Map<Long, User> getUserMap() {
		if (userMap == null) {
			userMap = new HashMap<Long, User>();
		}
		return userMap;
	}

	public void setUserMap(Map<Long, User> userMap) {
		this.userMap = userMap;
	}

	public WeakUserReport getUserReport(Long problemID) {
		for (WeakUserReport userReport : getYixianReportList()) {
			for (WeakProblem p : userReport.getProblemList()) {
				if (p.getId().equals(problemID)) {
					return userReport;
				}
			}
		}
		for (WeakUserReport userReport : getGuanliReportList()) {
			for (WeakProblem p : userReport.getProblemList()) {
				if (p.getId().equals(problemID)) {
					return userReport;
				}
			}
		}
		for (WeakUserReport userReport : getNotWrittenGuanliReportList()) {
			for (WeakProblem p : userReport.getProblemList()) {
				if (p.getId().equals(problemID)) {
					return userReport;
				}
			}
		}

		return null;
	}

	public void setNoneWorkTimeValue(Long problemID, String value) {
		WeakProblem problem = getProblem(problemID);
		if (problem == null) {
			// an error input, ignore it.
			return;
		}
		if (problem.hasNoneWorkTime()) {
			problem.setNoneWorkTime(value);
		} else {
			problem.setNoneWorkTime(null);
		}
	}

	public WeakProblem getProblem(Long problemID) {
		for (WeakUserReport userReport : getYixianReportList()) {
			for (WeakProblem p : userReport.getProblemList()) {
				if (p.getId().equals(problemID)) {
					return p;
				}
			}
		}
		for (WeakUserReport userReport : getGuanliReportList()) {
			for (WeakProblem p : userReport.getProblemList()) {
				if (p.getId().equals(problemID)) {
					return p;
				}
			}
		}
		for (WeakUserReport userReport : getNotWrittenGuanliReportList()) {
			for (WeakProblem p : userReport.getProblemList()) {
				if (p.getId().equals(problemID)) {
					return p;
				}
			}
		}
		for (WeakProblem p : getNeedDeleteList()) {
			if (p.getId().equals(problemID)) {
				return p;
			}
		}

		return null;
	}

}

final class UserReportComparatorByRoleLevel implements Comparator<WeakUserReport> {
	private static UserReportComparatorByRoleLevel instance;

	public static Comparator<WeakUserReport> getInstance() {
		if (instance == null) {
			instance = new UserReportComparatorByRoleLevel();
		}
		return instance;
	}

	private UserReportComparatorByRoleLevel() {

	}

	public int compare(WeakUserReport o1, WeakUserReport o2) {
		int compare = o2.getRoleType() - o1.getRoleType();
		if (compare == 0) {
			compare = o2.getUserID().intValue() - o1.getUserID().intValue();
		}

		return compare;
	}
}
